package com.farm.shop.controller;

import cn.hutool.core.convert.Convert;
import com.farm.common.annotation.Log;
import com.farm.common.utils.PageUtils;
import com.farm.common.utils.Query;
import com.farm.common.utils.R;
import com.farm.common.utils.ShiroUtils;
import com.farm.shop.dao.FarmShopDao;
import com.farm.shop.domain.FarmDishDO;
import com.farm.shop.domain.FarmRoomDO;
import com.farm.shop.domain.FarmShopDO;
import com.farm.shop.response.PlumDishDetail;
import com.farm.shop.service.FarmDishService;
import com.farm.shop.service.FarmShopService;
import com.farm.system.domain.UserDO;
import com.farm.utils.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 菜品
 *
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2019-09-06 19:44:49
 */

@Controller
@RequestMapping("/farm/farmDish")
public class FarmDishController {
    @Autowired
    private FarmDishService farmDishService;

    @Autowired
    private FarmShopService farmShopService;

    @Autowired
    FarmShopDao farmShopDao;

    @ResponseBody
    @GetMapping(value = "/app/dish/search")
    public Result getAppDishSearch(@RequestParam Map<String, Object> params) {
        Object keyword = params.get("dishKeyword");
        String dishKeyword = Convert.toStr(keyword);
        List<FarmDishDO> search = farmDishService.search(dishKeyword);
        if (search.size() > 0) {
            return Result.success(search);
        } else {
            return Result.success("抱歉，没有符合条件的美食");
        }

    }

    /**
     * 用户版   查询美食详情
     *
     * @return List<FarmDishDO>
     */
    @ResponseBody
    @GetMapping(value = "/app/dishdetail")
    public Result getAppDishDetailByid(@RequestParam Map<String, Object> params) {
        List<FarmDishDO> dishdetail = farmDishService.list(params);
        PlumDishDetail result = new PlumDishDetail();
        result.setWrapper(dishdetail);
        FarmShopDO farmShop = farmShopService.get(dishdetail.get(0).getShopId());
        result.setPhoneNumber(farmShop.getPhone());
        return Result.success(result);
    }

    /**
     * 根据商家 id 查询该商家的美食列表
     */
    @ResponseBody
    @GetMapping(value = "/app/dishs")
    public Result getAppDishByShopId(@RequestParam Map<String, Object> params) {

        //查询列表数据
        Query query = new Query(params);

        List<FarmDishDO> appDishsByShopId = farmDishService.list(query);
        return Result.success(appDishsByShopId);
    }

    @GetMapping()
    @RequiresPermissions("farm:farmDish:farmDish")
    String FarmDish() {
        UserDO user = ShiroUtils.getUser();
        // 16 代表平台管理员
        return  user.getDeptId() == 16 ? "farm/farmDish/platformFarmDish"
                : "farm/farmDish/farmDish";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("farm:farmDish:farmDish")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        UserDO user = ShiroUtils.getUser();
        Long userId = user.getUserId();
        Long shopId = null;

        // 判断角色
        if (user.getDeptId() != 16){
            params.put("userId", userId);
            Query query = new Query(params);
            List<FarmShopDO> farmShop = farmShopService.list(query);
            shopId = farmShop.get(0).getId();
        }

        if (shopId != null){
            params.put("shopId", shopId);
        }

        Query query = new Query(params);
        List<FarmDishDO> farmDishList = farmDishService.list(query);
        int total = farmDishService.count(query);
        PageUtils pageUtils = new PageUtils(farmDishList, total);
        return pageUtils;
    }

    @Log(value = "B模糊查询菜品列表")
    @ResponseBody
    @GetMapping("/searchName")
    @RequiresPermissions("farm:farmDish:farmDish")
    public PageUtils searchName(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<FarmDishDO> farmDishList = farmDishService.searchName(query);
        int total = farmDishService.searchNameCount(query);
        PageUtils pageUtils = new PageUtils(farmDishList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("farm:farmDish:add")
    String add() {
        return "farm/farmDish/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("farm:farmDish:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        FarmDishDO farmDish = farmDishService.get(id);
        String[] scenicpictureArr = farmDish.getDishPhotos().split(",");
        if (scenicpictureArr.length==1&&"".equals(scenicpictureArr[0])){
            scenicpictureArr = null;
        }
        model.addAttribute("farmDish", farmDish);
        model.addAttribute("scenicpictureArr", scenicpictureArr);
        return "farm/farmDish/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("farm:farmDish:add")
    public R save(FarmDishDO farmDish) {
        // 根据 userId 查询店铺 id
        Long userId = ShiroUtils.getUserId();
        Long shopId = farmShopDao.getShopId(userId);
        farmDish.setShopId(shopId);
        if (farmDishService.save(farmDish) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("farm:farmDish:edit")
    public R update(FarmDishDO farmDish) {
        farmDishService.update(farmDish);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("farm:farmDish:remove")
    public R remove(Long id) {
        if (farmDishService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("farm:farmDish:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] ids) {
        farmDishService.batchRemove(ids);
        return R.ok();
    }

    @GetMapping("/dishPreview/{id}")
    String dishPreview(@PathVariable("id") Long id, Model model) {
        FarmDishDO farmDish = farmDishService.get(id);
        String[] scenicpictureArr = farmDish.getDishPhotos().split(",");
        model.addAttribute("farmDish", farmDish);
        model.addAttribute("scenicpictureArr", scenicpictureArr);
        return "farm/farmDish/dishBanner";
    }

}
